package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MInventory;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.material.dao.MInventoryDao;
import com.zhongwang.mms.module.material.service.MInventoryEditService;
import com.zhongwang.mms.module.material.service.MInventoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import static com.zhongwang.mms.util.BeanUtil.copy;


/**
 * @Description: 库存信息编辑 控制层
 * @Author: China.wsg
 * @Date: 2020/05/09 10:15
 */
@Controller
@RequestMapping("/material/inventoryEdit")
public class MInventoryEditController extends BaseController {

    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private MInventoryEditService inventoryEditService;
    @Autowired
    private MInventoryDao mInventoryDao;

    /**
     * 库存信息编辑列表 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"matinv:matinvedit:view"})
    public String page(Model model) {
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
        return "material/matWarManage/InventoryEdit/page";
    }

    /**
     * 库存编辑
     *
     * @param inventory 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"matinv:matinvedit:edit"})
    public JsonResult edit(MInventory inventory) {
        inventoryEditService.edit(inventory);
        return success();
    }

    /**
     * 库存编辑校验（无更改、合并、不合并）
     *
     * @param inventory 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/editCheck")
    @RequiresPermissions({"matinv:matinvedit:edit"})
    public JsonResult editCheck(MInventory inventory) {

        JsonResult jsonResult = new JsonResult();
        MInventory oldInventory = mInventoryDao.selectByPrimaryKey(inventory.getItyId());

        MInventory newInventory = new MInventory();
        copy(oldInventory, newInventory);
        newInventory.setItyProName(inventory.getItyProName());
        newInventory.setItySpec(inventory.getItySpec());
        newInventory.setItyBackDate(inventory.getItyBackDate());

        String msg = inventoryEditService.editCheck(oldInventory, newInventory);
        if (msg.contains("合并") || msg.contains("不合并")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        } else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }


}
